<template>
  <div class="login">
    <div class="login-container">
      <div class="login-img">
        <img src="../assets/R-C.png" alt="login-img" />
      </div>
      <div class="login-form-wrap">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">{{title}}</h3>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
               type="primary" link 
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>


.login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('../assets/98ed1c8bc2d607edb19f8f32456cc433.png');
  background-size: cover;
  background-color: linear-gradient(135deg, #4f8cff 0%, #38c8fa 100%);
  position: relative;
}

.login-container {
  display: flex;
  width: 760px;
  background: rgba(255,255,255,0.08);
  border-radius: 18px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
  overflow: hidden;
}

.login-img {
  width: 50%;
  background: #ffff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-img img {
  width: 100%;
  height: 370px;
  border-radius: 12px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
}

.login-form-wrap {
  width: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.login-form {
  background: none;
  border-radius: 0;
  box-shadow: none;
  width: 320px;
  padding: 0;
  display: flex;
  flex-direction: column;
  z-index: 2;
}

.login-form {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
  width: 380px;
  padding: 0px 32px 0px 32px;
  display: flex;
  flex-direction: column;
  z-index: 2;
}

.title {
  text-align: center;
  font-size: 28px;
  font-weight: 700;
  color: #2563eb;
  // margin-bottom: 32px;
  letter-spacing: 2px;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-input {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  input {
    height: 44px;
    font-size: 16px;
    border-radius: 8px;
  }
}

.input-icon {
  height: 20px;
  width: 20px;
  margin-left: 2px;
}

.login-code {
  width: 35%;
  height: 44px;
  float: right;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    height: 44px;
  }
}

.el-checkbox {
  margin-bottom: 18px !important;
  font-size: 15px;
}

.el-button {
  border-radius: 8px !important;
  font-size: 18px;
  font-weight: 600;
  background: linear-gradient(90deg, #4f8cff 0%, #38c8fa 100%);
  border: none;
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  transition: background 0.3s;
}
.el-button:hover {
  background: linear-gradient(90deg, #2563eb 0%, #38c8fa 100%);
}

.link-type {
  color: #2563eb;
  font-size: 15px;
  margin-top: 8px;
  text-decoration: underline;
}

.el-login-footer {
  position: absolute;
  bottom: 24px;
  left: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 13px;
  letter-spacing: 1px;
  opacity: 0.8;
}
</style>
